var datagrid = null;
//定义全局ID ARR
var dictid = null;
var dictidArr = null;
//插入or修改标志:0,新增 1,修改
var upsert = 0;

$(function() {
	initDialog();
	initMsgDatagird();
	//var width = $("div .flexigrid").width();
	//var height = $("#leftDIV_z").height();
	//alert(height);
	$("#addDictA").bind("click", function(){
		upsert = 0;
		$("#ui-dialog-title-dictDialog").text("新增类型");
		MZ.openDialog("dictDialog");
	});
	
	$("#updateDictA").bind("click", function(){
		upsert = 1;
		dictid = null;
		MZ.clearForm("dictForm");
		var rowVals = datagrid.getRowValues();
		//reset
		if(rowVals.length == 1){
			rowVals = rowVals[0];
			dictid = rowVals.id;
			$("#dictNameText").val(rowVals.name);
			$("#dictdesc").val(rowVals.description);
			$("#deptSel").val(rowVals.code);  
			$("#ui-dialog-title-dictDialog").text("修改类型");
			MZ.openDialog("dictDialog");
		}else{
			MZ.showMsg("请选择一行记录修改!");
		}
	});
	
	$("#delDictA").bind("click", function(){
		dictidArr = null;
		dictidArr = datagrid.getRowValue(2);
		if(dictidArr.length >= 1){
			MZ.openDialog("dialog-confirm");
		}else{
			MZ.showMsg("请选择一行记录修改!");
		}
		
	});
	
	$("#submitBtn").bind("click", function(){
		if(upsert == 0){
			addDict();
		}else if(upsert == 1){
			updateDict();
		}
	});
	$("#resetBtn").bind("click", function(){
		MZ.clearForm("dictForm");
	});
	
	$("#searchA").bind("click", function(){
		searchDict();
	});
	
	$("#resetA").bind("click", function(){
		$("#dictnameText").val("");
	});
});

function initDialog(){
	//DIALOG//
	$('#dictDialog').dialog({
		autoOpen: false,
		width: 500,
		height: 400,
		modal: true
	});
	
	$( "#dialog-confirm" ).dialog({
		resizable: false,
		autoOpen: false,
		height:180,
		modal: true,
		buttons: {
			"确认删除": function() {
				delDict();
				$( this ).dialog( "close" );
			},
			"取消": function() {
				$( this ).dialog( "close" );
			}
		}
	});
}
//init message datagird
//get json data from server
function initMsgDatagird(){
	//var width = $("#searchBox").width()-0;
	datagrid = $("#msgDatagird").flexigrid({
		url: "dictmanager/getDictInfo",
		colModel : [
			{display: "ID", name : "id", width : 90, sortable : true, hide: true},
			{display: "类型名称", name : "name", width : 150, sortable : true, align: "center"},
			{display: "排序", name : "sort", width : 90, sortable : true, align: "center"},
			{display: "CODE", name : "code", width : 90, sortable : true, hide: true},
			{display: "所属模块", name : "module", width : 150, sortable : true, align: "center"},
			{display: "创建人", name : "creator", width : 120, sortable : true, align: "center"},
			{display: "创建时间", name : "gmtCreate", width : 150, sortable : true, align: "center"},
			{display: "描述", name : "description", width : 240, sortable : true, align: "left"}
		]
    });
}

//funciton onSelectRow required
//update flexigrid onselectrow function
//if you need't use onselect row
//let this function blank to do nothing
function onSelectRow(tr){
	
}


function searchDict(){
	var dictname = $("#dictNameText").val();
	var params = [{name:"dictname", value: dictname}];
	$("#msgDatagird").flexOptions({newp: 1, params: params}).flexReload();
}

function addDict(){
	var deptName = $("#deptSel").find("option:selected").text();
	var deptId = $("#deptSel").val(); 
	var params = $("#dictForm").formSerialize()+"&modual="+deptName+"&code="+deptId;
	var dictName = $("#dictNameText").val();
	if(!checkDict(dictName, deptName)) {
		return false;
	}
	$.ajax({
		url : "dictmanager/addDictInfo",
		// processData: false,
		type : "post",
		data : params,
		beforeSend : function() {
			MZ.clearForm("dictForm");
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
			MZ.closeDialog("dictDialog");
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

function updateDict(){
	var deptName = $("#deptSel").find("option:selected").text();
	var deptId = $("#deptSel").val(); 
	var params = $("#dictForm").formSerialize()+"&modual="+deptName+"&code="+deptId+"&dictid="+dictid;
	var dictName = $("#dictNameText").val();
	if(!checkDict(dictName, deptName)) {
		return false;
	}
	$.ajax({
		url : "dictmanager/updateDictInfo",
		// processData: false,
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
			MZ.closeDialog("dictDialog");
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

function delDict(){
	var params = {"ids":dictidArr.toString()};
	$.ajax({
		url : "dictmanager/delDictInfo",
		// processData: false,
		type : "post",
		data : params,
		beforeSend : function() {
		},
		success : function(result) {
			if(result.succeed){
				MZ.reloadDatagrid("msgDatagird");
			}else{
				MZ.showMsg(result.message);
			}
		},
		error: function(result){
			MZ.showMsg(result);
		}
	});
}

//VALID FORM
function checkDict(dictName, deptName){
	if($.trim(dictName)==""){
		MZ.showMsg("系统字段名称不能为空!");
		return false;
	}
	if($.trim(deptName)==""){
		MZ.showMsg("请选择系统字典所属模块!");
		return false;
	}
	return true;
}
